//
//  VideoCanvasItem.swift
//  Hermes
//
//  Created by FH on 2021/1/17.
//  Copyright © 2021 Agora. All rights reserved.
//

import Foundation
// TODO: remove
class VideoCanvasItem {
    var isStartRender = false
    var index: Int = 0
    var canvas: AgoraRtcVideoCanvas!
    var bgView: FLCommonView!
    var videoPanel: NSView!
    var lblIndex: NSTextField!
    var lblUID: NSTextField!
    var coverView: NSView!
    
    func setupCanvas(uid: UInt, forLocal: Bool, _ agoraKit: AgoraRtcEngineKit) {
        if !self.isStartRender {
            self.isStartRender = true
            self.canvas = AgoraRtcVideoCanvas()
            canvas.renderMode = .hidden
            canvas.uid = uid
            canvas.view = self.videoPanel
            self.lblUID.stringValue = "uid: \(uid)"
            if forLocal {
                agoraKit.setupLocalVideo(self.canvas)
            } else {
                agoraKit.setupRemoteVideo(self.canvas)
            }
        }
    }
    
    func removeVideoRenderView(default_lblUID_text: String) {
        if self.isStartRender {
            if let subViews = self.videoPanel?.subviews {
                var renderView: NSView!
                for sub in subViews {
                    if !(sub is NSTextField) {
                        renderView = sub
                        break
                    }
                }
                // remove AgoraRtcVideoCanvas's renderView
                if let renderView = renderView {
                    renderView.removeFromSuperview()
                }
            }
            self.canvas = nil
            self.lblUID.stringValue = default_lblUID_text
            self.isStartRender = false
        }
    }
}

extension VideoCanvasItem : CustomStringConvertible {
    var description: String {
        return "item: \(self.index)"
    }
}

////////////////////////////

class GridVideosVC : NSViewController {
    private let yoga = FLContext()
    private var size: CGSize! = nil
    private var cellDim: CGFloat! = nil
    private var startPos: Int! = nil
    private var pageSize = 0
    private var onBuildVideoItemFn: Action1<VideoCanvasItem>! = nil
    
    convenience init(startPos: Int, pageSize: Int=50,
                     cellDim: CGFloat, layoutSize: CGSize,
                     _ onBuildVideoItemFn: @escaping Action1<VideoCanvasItem>) {
        self.init(nibName: nil, bundle: nil)
        
        self.size = CGSize(width: layoutSize.width + 2, height: layoutSize.height + 2)
        self.cellDim = cellDim
        self.startPos = startPos
        self.pageSize = pageSize
        self.onBuildVideoItemFn = onBuildVideoItemFn
    }
    
    override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func loadView() {
        self.view = NSView(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
    }
    
    override func viewDidLoad() {
        self.view.bindAsRoot(to: yoga) { it in
            it.paddingLeft = 2
            it.paddingTop = 2
            it.wrap = .wrap
            it.direction = .row
        }
        
        let count = self.pageSize >= 100 ? 28 : self.pageSize
        for i in 0..<count {
            let item = buildVideoStreamItem(cellDim: cellDim, cellName: "\(self.startPos+i+1)", 2, 13, yoga, self.view)
            item.index = self.startPos + i
            self.onBuildVideoItemFn(item)
        }
        
        yoga.apply()
    }
    
    func getItemIndexRange() -> Range<Int> {
        if self.startPos >= 100  {
            return self.startPos..<128
        } else {
            return self.startPos..<(self.startPos + self.pageSize)
        }
    }
}

////////////////////////////

func buildVideoStreamItem(cellDim: CGFloat,
                          cellWidthDim: CGFloat?=nil, uidHeight: CGFloat?=nil, cellName: String,
                          _ cellMargin: CGFloat, _ extraInfoHeight: CGFloat, extraInfoFontSize: CGFloat=10,
                          _ yoga: FLContext, _ superView: FLView) -> VideoCanvasItem {
    let item = VideoCanvasItem()
    
    item.bgView = FLCommonView()
    item.bgView.bind(to: yoga, superView) { it in
        it.marginRight = FLValue(cellMargin)
        it.marginBottom = FLValue(cellMargin)
    }

    let width = cellWidthDim ?? cellDim
    let height = cellDim
    item.videoPanel = NSView()
    item.videoPanel.bind(to: yoga, item.bgView) { it in
        it.width = width.toFLValue()
        it.height = height.toFLValue()
        it.justify = .center
        it.alignItems = .center
    }
    item.videoPanel.flBackgroundColor = .lightGray

    item.lblIndex = FLLabel()
    item.lblIndex.bind(to: yoga, item.videoPanel)
    item.lblIndex.font = NSFont.systemFont(ofSize: 12)
    item.lblIndex.flText = cellName
    
    item.lblUID = FLLabel()
    item.lblUID.bind(to: yoga, item.bgView) { it in
        it.width = width.toFLValue()
        it.height = extraInfoHeight.toFLValue()
    }
    item.lblUID.font = NSFont.systemFont(ofSize: extraInfoFontSize)
    item.lblUID.flText = "uid: ..."
    item.lblUID.flBackgroundColor = .gray

    item.coverView = NSView()
    item.coverView.flTag = 181818
    item.coverView.bind(to: yoga, item.bgView) { it in
        it.position = .absolute
        it.left = 0
        it.top = 0
        it.width = width.toFLValue()
        it.height = height.toFLValue()
    }
    return item
}
